//
//  NoticeView.m
//  deerkids
//
//  Created by zhangxy on 2017/12/13.
//  Copyright © 2017年 wangzhao. All rights reserved.
//

#import "NoticeView.h"


@interface NoticeView()
{
    
}

@property (nonatomic,strong)  UILabel * labTitle ; // 当前时间
@property (nonatomic,strong)  UILabel * labMessage ; // 当前时间
@property (nonatomic,strong)  NSDictionary * userInfo ; // 当前时间

@end

@implementation NoticeView

-(id)initWitMessageInfo:(NSDictionary *) info{
    _userInfo = info;
    return [self initWithFrame:CGRectMake(10, StatusBarHeight, SCREEN_WIDTH-20, 64)];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
        [self uiConfigWith:frame];
        
        self.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pageClick:)];
        [self addGestureRecognizer:tap];
    }
    return self;
}


-(void)uiConfigWith:(CGRect)bframe
{
    self.backgroundColor= UIColor.whiteColor;
    self.layer.cornerRadius = 5.0f;
    self.layer.masksToBounds = YES;
    
    
    _labTitle = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH - 40, 30)];
    
    _labTitle.textColor = DEEP_GRAY_COLOR;
    _labTitle.font = [UIFont boldSystemFontOfSize:12];
    _labTitle.text = convertToString(_userInfo[@"aps"][@"alert"][@"title"]);
    [self addSubview:_labTitle];
    
    UIImageView *bgView = [[UIImageView alloc] init];
    [bgView setBackgroundColor:BG_GRAY_COLOR];
    [bgView setFrame:CGRectMake(0, 30, SCREEN_WIDTH-20, 34)];
    [self addSubview:bgView];
    
    _labMessage = [[UILabel alloc]initWithFrame:CGRectMake(10, 34, SCREEN_WIDTH - 40, 26)];
    _labMessage.numberOfLines = 0;
    _labMessage.textColor = DEEP_GRAY_COLOR;
    _labMessage.font = [UIFont systemFontOfSize:14];
    _labMessage.text = convertToString(_userInfo[@"aps"][@"alert"][@"body"]);
    [self addSubview:_labMessage];
    
    CGSize s = [self autoHeightOfLabel:_labMessage with:SCREEN_WIDTH - 40];
    CGFloat x = 0;
    if(s.height > 26){
        x = s.height - 26 + 5;
    }
    
    [self setFrame:CGRectMake(10, StatusBarHeight, SCREEN_WIDTH - 20, 64+x)];
    [bgView setFrame:CGRectMake(0, 30, SCREEN_WIDTH-20, 34+x)];
}


/**
 计算Label高度
 
 @param label 要计算的label，设置了值
 @param width label的最大宽度
 */
- (CGSize )autoHeightOfLabel:(UILabel *)label with:(CGFloat )width{
    //Calculate the expected size based on the font and linebreak mode of your label
    // FLT_MAX here simply means no constraint in height
    CGSize maximumLabelSize = CGSizeMake(width, FLT_MAX);
    
    CGSize expectedLabelSize = [label sizeThatFits:maximumLabelSize];
    
    //adjust the label the the new height.
    CGRect newFrame = label.frame;
    newFrame.size.height = expectedLabelSize.height;
    label.frame = newFrame;
    [label updateConstraintsIfNeeded];
    
    return expectedLabelSize;
}

-(void)pageClick:(UITapGestureRecognizer *) tap{
    
    NSString *jsonStr = convertToString([_userInfo objectForKey:@"payload"]);
    NSDictionary *dic = [self dictionaryWithJsonString:jsonStr];
    
    if(_ResultBlock){
        _ResultBlock(dic[@"action"]);
    }
    
    [self dissmissInWindow];
    
}
#pragma mark - 获取Oss信息
- (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    if (jsonString == nil) {
        return nil;
    }
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}


-(void)showInWindow{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    [NSTimer scheduledTimerWithTimeInterval:5.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
        [self dissmissInWindow];
    }];
}

-(void)dissmissInWindow{
    [self removeFromSuperview];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
